#include "stdafx.h"
#include "CharacterModel.h"
#include "moc_CharacterModel.cpp"
#include "SkillModel.h"
#include "ExtraordinaryAbilityModel.h"
#include "WeaponProficiencyModel.h"
#include "EquipmentModel.h"
#include <QtXml\QDomDocument>
#include <QtCore/QUrl>

#pragma region Constructor

CharacterModel::CharacterModel(void):
	_IQ(0),
	_ME(0),
	_MA(0),
	_PS(0),
	_PP(0),
	_PE(0),
	_PB(0),
	_SPD(0),
	_HP(0),
	_SDC(0),
	_NumAttacks(0),
	_Strike(0),
	_Damage(0),
	_Kick(0),
	_Parry(0),
	_Roll(0),
	_Dodge(0),
	_Critical(0),
	_CurrentHP(0),
	_CurrentSDC(0)
{

}

CharacterModel::~CharacterModel(void)
{
	while (!_PrimarySkills.isEmpty())
		delete _PrimarySkills.takeFirst();

	while (!_SecondarySkills.isEmpty())
		delete _SecondarySkills.takeFirst();

	while (!_ExtraordinaryAbilities.isEmpty())
		delete _ExtraordinaryAbilities.takeFirst();

	while (!_WeaponProficiencies.isEmpty())
		delete _WeaponProficiencies.takeFirst();

	while (!_Equipment.isEmpty())
		delete _Equipment.takeFirst();
}

#pragma endregion

#pragma region Properties

#pragma endregion

#pragma region Public Methods and Operators

bool CharacterModel::Serialize(QDomDocument * doc, QDomElement * tag)
{
	SerializeProps(doc, tag);

	QDomElement tagImg = doc->createElement("Image");
	tag->appendChild(tagImg);
	QDomCDATASection cdImg = doc->createCDATASection(QString::fromLatin1(Image().toBase64().data()));
	tagImg.appendChild(cdImg);

	QDomElement tagPrim = doc->createElement("PrimarySkills");
	tag->appendChild(tagPrim);
	SerializeList<SkillModel>(doc, &tagPrim, PrimarySkills(), "Skill");

	QDomElement tagSec = doc->createElement("SecondarySkills");
	tag->appendChild(tagSec);
	SerializeList<SkillModel>(doc, &tagSec, SecondarySkills(), "Skill");

	QDomElement tagEx = doc->createElement("ExtraordinaryAbilities");
	tag->appendChild(tagEx);
	SerializeList<ExtraordinaryAbilityModel>(doc, &tagEx, ExtraordinaryAbilities(), "ExtraordinaryAbility");

	QDomElement tagWP = doc->createElement("WeaponProficiencies");
	tag->appendChild(tagWP);
	SerializeList<WeaponProficiencyModel>(doc, &tagWP, WeaponProficiencies(), "WeaponProficiency");

	QDomElement tagE = doc->createElement("Equipment");
	tag->appendChild(tagE);
	SerializeList<EquipmentModel>(doc, &tagE, Equipment(), "Equipment");

	return true;
}

bool CharacterModel::Deserialize(QDomDocument * doc, QDomElement * tag)
{
	DeserializeProps(doc, tag);

	QDomElement tagImg = tag->firstChildElement("Image");
	QDomCDATASection cdImg = tagImg.firstChild().toCDATASection();
	if (!cdImg.isNull())
	{
		setImage(QByteArray::fromBase64(cdImg.data().toLatin1()));
	}

	QDomElement tagPrim = tag->firstChildElement("PrimarySkills");
	DeserializeList<SkillModel>(doc, &tagPrim, PrimarySkills(), "Skill");

	QDomElement tagSec = tag->firstChildElement("SecondarySkills");
	DeserializeList<SkillModel>(doc, &tagSec, SecondarySkills(), "Skill");

	QDomElement tagEx = tag->firstChildElement("ExtraordinaryAbilities");
	DeserializeList<ExtraordinaryAbilityModel>(doc, &tagEx, ExtraordinaryAbilities(), "ExtraordinaryAbility");

	QDomElement tagWP = tag->firstChildElement("WeaponProficiencies");
	DeserializeList<WeaponProficiencyModel>(doc, &tagWP, WeaponProficiencies(), "WeaponProficiency");

	QDomElement tagE = tag->firstChildElement("Equipment");
	DeserializeList<EquipmentModel>(doc, &tagE, Equipment(), "Equipment");

	return true;
}

#pragma endregion

#pragma region Helper Methods

#pragma endregion
